<template>
  <div class="addEdit-block"
       :style='{"padding":"10px 30px 30px","background":"url(http://codegen.caihongy.cn/20230228/47752d6be3a94e8892a83ec2a70c8443.png)"}'
       style="width: 100%;">
    <el-form
        :style='{"border":"1px solid #ddd","padding":"30px","boxShadow":"0 0px 0px #ddd","borderRadius":"5px","background":"#ffffff"}'
        class="add-update-preview"
        ref="ruleForm"
        :model="ruleForm"
        :rules="rules"
        label-width="180px"
    >
      <template>
        <el-form-item :style='{"margin":"0 0 20px 0"}' class="input" v-if="type!='info'" label="课程名称"
                      prop="kechengmingcheng">
<!--          <el-input v-model="ruleForm.kechengmingcheng" placeholder="课程名称" clearable-->
<!--                    :readonly="ro.kechengmingcheng"></el-input>-->
          <el-select :disabled="ro.kechengmingcheng" v-model="ruleForm.kechengmingcheng" placeholder="请选择课程名称">
            <el-option
                v-for="(item,index) in kechengmingchengOptions"
                v-bind:key="index"
                :label="item"
                :value="item">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :style='{"margin":"0 0 20px 0"}' v-else class="input" label="课程名称" prop="kechengmingcheng">
          <el-input v-model="ruleForm.kechengmingcheng" placeholder="课程名称" readonly></el-input>
        </el-form-item>
<!--        <el-form-item :style='{"margin":"0 0 20px 0"}' class="select" v-if="type!='info'" label="课程类型"-->
<!--                      prop="kechengleixing">-->
<!--          <el-select :disabled="ro.kechengleixing" v-model="ruleForm.kechengleixing" placeholder="请选择课程类型">-->
<!--            <el-option-->
<!--                v-for="(item,index) in kechengleixingOptions"-->
<!--                v-bind:key="index"-->
<!--                :label="item"-->
<!--                :value="item">-->
<!--            </el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
<!--        <el-form-item :style='{"margin":"0 0 20px 0"}' v-else class="input" label="课程类型" prop="kechengleixing">-->
<!--          <el-input v-model="ruleForm.kechengleixing"-->
<!--                    placeholder="课程类型" readonly></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item :style='{"margin":"0 0 20px 0"}' class="upload" v-if="type!='info' && !ro.fengmiantu"-->
<!--                      label="封面图" prop="fengmiantu">-->
<!--          <file-upload-->
<!--              tip="点击上传封面图"-->
<!--              action="file/upload"-->
<!--              :limit="3"-->
<!--              :multiple="true"-->
<!--              :fileUrls="ruleForm.fengmiantu?ruleForm.fengmiantu:''"-->
<!--              @change="fengmiantuUploadChange"-->
<!--          ></file-upload>-->
<!--        </el-form-item>-->
        <el-form-item :style='{"margin":"0 0 20px 0"}' class="upload" v-else-if="ruleForm.fengmiantu" label="封面图"
                      prop="fengmiantu">
          <img v-if="ruleForm.fengmiantu.substring(0,4)=='http'" class="upload-img" style="margin-right:20px;"
               v-bind:key="index" :src="ruleForm.fengmiantu.split(',')[0]" width="100" height="100">
          <img v-else class="upload-img" style="margin-right:20px;" v-bind:key="index"
               v-for="(item,index) in ruleForm.fengmiantu.split(',')" :src="$base.url+item" width="100" height="100">
        </el-form-item>
        <el-form-item :style='{"margin":"0 0 20px 0"}' class="upload" v-if="type!='info'&& !ro.kechengshipin"
                      label="课程视频" prop="kechengshipin">
          <file-upload
              tip="点击上传课程视频"
              action="file/upload"
              :limit="30"
              :multiple="true"
              :fileUrls="ruleForm.kechengshipin?ruleForm.kechengshipin:''"
              @change="kechengshipinUploadChange"
          ></file-upload>
        </el-form-item>
        <el-form-item :style='{"margin":"0 0 20px 0"}' v-else-if="ruleForm.kechengshipin" label="课程视频"
                      prop="kechengshipin">
          <!--					<el-button :style='{"border":"1px solid #3ca512","cursor":"pointer","padding":"0 35px","margin":"0 20px 0 0","outline":"none","color":"rgba(255, 255, 255, 1)","borderRadius":"4px","background":"url(http://codegen.caihongy.cn/20221011/ca1c191554d24b108bc94f4a2046d636.png) #41b314 no-repeat 15px 12px","width":"auto","lineHeight":"40px","fontSize":"14px","height":"40px"}' type="text" size="small" @click="download(ruleForm.kechengshipin)">-->
          <!--            预览</el-button>-->
          <div v-for="(item,i) in ruleForm.kechengshipin.split(',')">
            <el-link type="primary" :href="$base.url + item" target="_blank">第{{ i + 1 }}个视频</el-link>
          </div>
        </el-form-item>
        <!--				<el-form-item :style='{"margin":"0 0 20px 0"}' v-else-if="!ruleForm.kechengshipin" label="课程视频" prop="kechengshipin">-->
        <!--					<el-button :style='{"border":"1px solid #3ca512","cursor":"pointer","padding":"0 35px","margin":"0 20px 0 0","outline":"none","color":"rgba(255, 255, 255, 1)","borderRadius":"4px","background":"url(http://codegen.caihongy.cn/20221011/ca1c191554d24b108bc94f4a2046d636.png) #41b314 no-repeat 15px 12px","width":"auto","lineHeight":"40px","fontSize":"14px","height":"40px"}' type="text" size="small">无</el-button>-->
        <!--				</el-form-item>-->
        <el-form-item :style='{"margin":"0 0 20px 0"}' class="date" v-if="type!='info'" label="发布日期"
                      prop="faburiqi">
          <el-date-picker
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd"
              v-model="ruleForm.faburiqi"
              type="date"
              :readonly="ro.faburiqi"
              placeholder="发布日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item :style='{"margin":"0 0 20px 0"}' class="input" v-else-if="ruleForm.faburiqi" label="发布日期"
                      prop="faburiqi">
          <el-input v-model="ruleForm.faburiqi" placeholder="发布日期" readonly></el-input>
        </el-form-item>
        <el-form-item :style='{"margin":"0 0 20px 0"}' class="input" v-if="type!='info'" label="工号" prop="gonghao">
          <el-input v-model="ruleForm.gonghao" placeholder="工号" clearable :readonly="ro.gonghao"></el-input>
        </el-form-item>
        <el-form-item :style='{"margin":"0 0 20px 0"}' v-else class="input" label="工号" prop="gonghao">
          <el-input v-model="ruleForm.gonghao" placeholder="工号" readonly></el-input>
        </el-form-item>
        <el-form-item :style='{"margin":"0 0 20px 0"}' class="input" v-if="type!='info'" label="教师姓名"
                      prop="jiaoshixingming">
          <el-input v-model="ruleForm.jiaoshixingming" placeholder="教师姓名" clearable
                    :readonly="ro.jiaoshixingming"></el-input>
        </el-form-item>
        <el-form-item :style='{"margin":"0 0 20px 0"}' v-else class="input" label="教师姓名" prop="jiaoshixingming">
          <el-input v-model="ruleForm.jiaoshixingming" placeholder="教师姓名" readonly></el-input>
        </el-form-item>
      </template>
<!--      <el-form-item :style='{"margin":"0 0 20px 0"}' class="textarea" v-if="type!='info'" label="课程目录"-->
<!--                    prop="kechengmulu">-->
<!--        <el-input-->
<!--            style="min-width: 200px; max-width: 600px;"-->
<!--            type="textarea"-->
<!--            :rows="8"-->
<!--            placeholder="课程目录"-->
<!--            v-model="ruleForm.kechengmulu">-->
<!--        </el-input>-->
<!--      </el-form-item>-->
<!--      <el-form-item :style='{"margin":"0 0 20px 0"}' v-else-if="ruleForm.kechengmulu" label="课程目录"-->
<!--                    prop="kechengmulu">-->
<!--        <span-->
<!--            :style='{"fontSize":"14px","lineHeight":"40px","color":"#333","fontWeight":"500","display":"inline-block"}'>{{ ruleForm.kechengmulu }}</span>-->
<!--      </el-form-item>-->
      <el-form-item :style='{"margin":"0 0 20px 0"}' v-if="type!='info'" label="视频介绍" prop="shipinjieshao">
        <editor
            style="min-width: 200px; max-width: 600px;"
            v-model="ruleForm.shipinjieshao"
            class="editor"
            action="file/upload">
        </editor>
      </el-form-item>
      <el-form-item :style='{"margin":"0 0 20px 0"}' v-else-if="ruleForm.shipinjieshao" label="视频介绍"
                    prop="shipinjieshao">
        <span
            :style='{"fontSize":"14px","lineHeight":"40px","color":"#333","fontWeight":"500","display":"inline-block"}'
            v-html="ruleForm.shipinjieshao"></span>
      </el-form-item>
      <el-form-item :style='{"padding":"0","margin":"0"}' class="btn">
        <el-button
            :style='{"border":"0","cursor":"pointer","padding":"0","margin":"0 20px 0 0","outline":"none","color":"rgba(255, 255, 255, 1)","borderRadius":"4px","background":"#337ab7","width":"128px","lineHeight":"40px","fontSize":"14px","height":"40px"}'
            v-if="type!='info'" type="primary" class="btn-success" @click="onSubmit">提交
        </el-button>
        <el-button
            :style='{"border":"1px solid #1b5a90","cursor":"pointer","padding":"0","margin":"0","outline":"none","color":"#1b5a90","borderRadius":"4px","background":"rgba(255, 255, 255, 1)","width":"128px","lineHeight":"40px","fontSize":"14px","height":"40px"}'
            v-if="type!='info'" class="btn-close" @click="back()">取消
        </el-button>
        <el-button
            :style='{"border":"1px solid #1b5a90","cursor":"pointer","padding":"0","margin":"0","outline":"none","color":"#1b5a90","borderRadius":"4px","background":"rgba(255, 255, 255, 1)","width":"128px","lineHeight":"40px","fontSize":"14px","height":"40px"}'
            v-if="type=='info'" class="btn-close" @click="back()">返回
        </el-button>
      </el-form-item>
    </el-form>


  </div>
</template>
<script>
// 数字，邮件，手机，url，身份证校验
import {isNumber, isIntNumer, isEmail, isPhone, isMobile, isURL, checkIdCard} from "@/utils/validate";

export default {
  data() {
    let self = this
    var validateIdCard = (rule, value, callback) => {
      if (!value) {
        callback();
      } else if (!checkIdCard(value)) {
        callback(new Error("请输入正确的身份证号码"));
      } else {
        callback();
      }
    };
    var validateUrl = (rule, value, callback) => {
      if (!value) {
        callback();
      } else if (!isURL(value)) {
        callback(new Error("请输入正确的URL地址"));
      } else {
        callback();
      }
    };
    var validateMobile = (rule, value, callback) => {
      if (!value) {
        callback();
      } else if (!isMobile(value)) {
        callback(new Error("请输入正确的手机号码"));
      } else {
        callback();
      }
    };
    var validatePhone = (rule, value, callback) => {
      if (!value) {
        callback();
      } else if (!isPhone(value)) {
        callback(new Error("请输入正确的电话号码"));
      } else {
        callback();
      }
    };
    var validateEmail = (rule, value, callback) => {
      if (!value) {
        callback();
      } else if (!isEmail(value)) {
        callback(new Error("请输入正确的邮箱地址"));
      } else {
        callback();
      }
    };
    var validateNumber = (rule, value, callback) => {
      if (!value) {
        callback();
      } else if (!isNumber(value)) {
        callback(new Error("请输入数字"));
      } else {
        callback();
      }
    };
    var validateIntNumber = (rule, value, callback) => {
      if (!value) {
        callback();
      } else if (!isIntNumer(value)) {
        callback(new Error("请输入整数"));
      } else {
        callback();
      }
    };
    return {
      id: '',
      type: '',


      ro: {
        kechengmingcheng: false,
        kechengleixing: false,
        fengmiantu: false,
        kechengshipin: false,
        kechengmulu: false,
        shipinjieshao: false,
        faburiqi: false,
        gonghao: false,
        jiaoshixingming: false,
        sfsh: false,
        shhf: false,
        clicktime: false,
      },


      ruleForm: {
        kechengmingcheng: '',
        kechengleixing: '',
        fengmiantu: '',
        kechengshipin: [],
        kechengmulu: '',
        shipinjieshao: '',
        faburiqi: '',
        gonghao: '',
        jiaoshixingming: '',
        shhf: '',
        clicktime: '',
      },

      kechengleixingOptions: [],
      kechengmingchengOptions:[],

      rules: {
        kechengmingcheng: [
          {required: true, message: '课程名称不能为空', trigger: 'blur'},
        ],
        kechengleixing: [
          {required: false, message: '课程类型不能为空', trigger: 'blur'},
        ],
        fengmiantu: [],
        kechengshipin: [],
        kechengmulu: [],
        shipinjieshao: [],
        faburiqi: [],
        gonghao: [],
        jiaoshixingming: [],
        sfsh: [],
        shhf: [],
        clicktime: [],
      }
    };
  },
  props: ["parent"],
  computed: {},
  components: {},
  created() {
    this.ruleForm.faburiqi = this.getCurDate()
  },
  methods: {

    // 下载
    download(file) {
      // console.log(this.$base)
      // http://127.0.0.1:8080/springboot13h1h/upload/1682941765044.mp4,upload/1683094529587.mp4
      file.split(',').forEach(item => {
        var file = this.$base.url + item;
        window.open(`${file}`)
      })

    },
    // 初始化
    init(id, type) {
      if (id) {
        this.id = id;
        this.type = type;
      }
      if (this.type == 'info' || this.type == 'else') {
        this.info(id);
      } else if (this.type == 'logistics') {
        this.logistics = false;
        this.info(id);
      } else if (this.type == 'cross') {
        var obj = this.$storage.getObj('crossObj');
        for (var o in obj) {
          if (o == 'kechengmingcheng') {
            this.ruleForm.kechengmingcheng = obj[o];
            this.ro.kechengmingcheng = true;
            continue;
          }
          if (o == 'kechengleixing') {
            this.ruleForm.kechengleixing = obj[o];
            this.ro.kechengleixing = true;
            continue;
          }
          if (o == 'fengmiantu') {
            this.ruleForm.fengmiantu = obj[o];
            this.ro.fengmiantu = true;
            continue;
          }
          if (o == 'kechengshipin') {
            this.ruleForm.kechengshipin = obj[o];
            this.ro.kechengshipin = true;
            continue;
          }
          if (o == 'kechengmulu') {
            this.ruleForm.kechengmulu = obj[o];
            this.ro.kechengmulu = true;
            continue;
          }
          if (o == 'shipinjieshao') {
            this.ruleForm.shipinjieshao = obj[o];
            this.ro.shipinjieshao = true;
            continue;
          }
          if (o == 'faburiqi') {
            this.ruleForm.faburiqi = obj[o];
            this.ro.faburiqi = true;
            continue;
          }
          if (o == 'gonghao') {
            this.ruleForm.gonghao = obj[o];
            this.ro.gonghao = true;
            continue;
          }
          if (o == 'jiaoshixingming') {
            this.ruleForm.jiaoshixingming = obj[o];
            this.ro.jiaoshixingming = true;
            continue;
          }
          if (o == 'clicktime') {
            this.ruleForm.clicktime = obj[o];
            this.ro.clicktime = true;
            continue;
          }
        }


      }


      // 获取用户信息
      this.$http({
        url: `${this.$storage.get('sessionTable')}/session`,
        method: "get"
      }).then(({data}) => {
        if (data && data.code === 0) {

          var json = data.data;
          if (this.$storage.get("role") != "管理员") {
            this.ro.faburiqi = true;
          }
          if (((json.gonghao != '' && json.gonghao) || json.gonghao == 0) && this.$storage.get("role") != "管理员") {
            this.ruleForm.gonghao = json.gonghao
            this.ro.gonghao = true;
          }
          if (((json.jiaoshixingming != '' && json.jiaoshixingming) || json.jiaoshixingming == 0) && this.$storage.get("role") != "管理员") {
            this.ruleForm.jiaoshixingming = json.jiaoshixingming
            this.ro.jiaoshixingming = true;
          }
        } else {
          this.$message.error(data.msg);
        }
      });

      this.$http({
        url: `option/kechengleixing/kechengleixing`,
        method: "get"
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.kechengleixingOptions = data.data;
        } else {
          this.$message.error(data.msg);
        }
      });

      this.$http({
        url: `option/kechengleixing/kechengleixing`,
        method: "get"
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.kechengleixingOptions = data.data;
        } else {
          this.$message.error(data.msg);
        }
      });

      this.$http({
        url: "/kechengxinxi/lists",
        method: "get"
      }).then(({data}) => {
        if (data && data.code === 0) {
          data.data.forEach(item=>{
            this.kechengmingchengOptions.push(item.kechengmingcheng)
          })
        } else {
          this.$message.error(data.msg);
        }
      });



    },
    // 多级联动参数

    info(id) {
      this.$http({
        url: `kechengshipin/info/${id}`,
        method: "get"
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.ruleForm = data.data;
          //解决前台上传图片后台不显示的问题
          let reg = new RegExp('../../../upload', 'g')//g代表全部
          this.ruleForm.shipinjieshao = this.ruleForm.shipinjieshao.replace(reg, '../../../springboot13h1h/upload');
        } else {
          this.$message.error(data.msg);
        }
      });
    },


    // 提交
    onSubmit() {


      if (this.ruleForm.fengmiantu != null) {
        this.ruleForm.fengmiantu = this.ruleForm.fengmiantu.replace(new RegExp(this.$base.url, "g"), "");
      }


      if (this.ruleForm.kechengshipin != null) {
        this.ruleForm.kechengshipin = this.ruleForm.kechengshipin.replace(new RegExp(this.$base.url, "g"), "");
      }


      var objcross = this.$storage.getObj('crossObj');

      //更新跨表属性
      var crossuserid;
      var crossrefid;
      var crossoptnum;
      if (this.type == 'cross') {
        var statusColumnName = this.$storage.get('statusColumnName');
        var statusColumnValue = this.$storage.get('statusColumnValue');
        if (statusColumnName != '') {
          var obj = this.$storage.getObj('crossObj');
          if (statusColumnName && !statusColumnName.startsWith("[")) {
            for (var o in obj) {
              if (o == statusColumnName) {
                obj[o] = statusColumnValue;
              }
            }
            var table = this.$storage.get('crossTable');
            this.$http({
              url: `${table}/update`,
              method: "post",
              data: obj
            }).then(({data}) => {
            });
          } else {
            crossuserid = this.$storage.get('userid');
            crossrefid = obj['id'];
            crossoptnum = this.$storage.get('statusColumnName');
            crossoptnum = crossoptnum.replace(/\[/, "").replace(/\]/, "");
          }
        }
      }
      this.$refs["ruleForm"].validate(valid => {
        if (valid) {
          if (crossrefid && crossuserid) {
            this.ruleForm.crossuserid = crossuserid;
            this.ruleForm.crossrefid = crossrefid;
            let params = {
              page: 1,
              limit: 10,
              crossuserid: this.ruleForm.crossuserid,
              crossrefid: this.ruleForm.crossrefid,
            }
            this.$http({
              url: "kechengshipin/page",
              method: "get",
              params: params
            }).then(({
                       data
                     }) => {
              if (data && data.code === 0) {
                if (data.data.total >= crossoptnum) {
                  this.$message.error(this.$storage.get('tips'));
                  return false;
                } else {
                  this.$http({
                    url: `kechengshipin/${!this.ruleForm.id ? "save" : "update"}`,
                    method: "post",
                    data: this.ruleForm
                  }).then(({data}) => {
                    if (data && data.code === 0) {
                      this.$message({
                        message: "操作成功",
                        type: "success",
                        duration: 1500,
                        onClose: () => {
                          this.parent.showFlag = true;
                          this.parent.addOrUpdateFlag = false;
                          this.parent.kechengshipinCrossAddOrUpdateFlag = false;
                          this.parent.search();
                          this.parent.contentStyleChange();
                        }
                      });
                    } else {
                      this.$message.error(data.msg);
                    }
                  });

                }
              } else {
              }
            });
          } else {
            this.$http({
              url: `kechengshipin/${!this.ruleForm.id ? "save" : "update"}`,
              method: "post",
              data: this.ruleForm
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: "操作成功",
                  type: "success",
                  duration: 1500,
                  onClose: () => {
                    this.parent.showFlag = true;
                    this.parent.addOrUpdateFlag = false;
                    this.parent.kechengshipinCrossAddOrUpdateFlag = false;
                    this.parent.search();
                    this.parent.contentStyleChange();
                  }
                });
              } else {
                this.$message.error(data.msg);
              }
            });
          }
        }
      });
    },
    // 获取uuid
    getUUID() {
      return new Date().getTime();
    },
    // 返回
    back() {
      this.parent.showFlag = true;
      this.parent.addOrUpdateFlag = false;
      this.parent.kechengshipinCrossAddOrUpdateFlag = false;
      this.parent.contentStyleChange();
    },
    fengmiantuUploadChange(fileUrls) {
      this.ruleForm.fengmiantu = fileUrls;
    },
    kechengshipinUploadChange(fileUrls) {
      this.ruleForm.kechengshipin = fileUrls;
    },
  }
};
</script>
<style lang="scss" scoped>
.amap-wrapper {
  width: 100%;
  height: 500px;
}

.search-box {
  position: absolute;
}

.el-date-editor.el-input {
  width: auto;
}

.add-update-preview .el-form-item /deep/ .el-form-item__label {
  padding: 0 10px 0 0;
  color: #666;
  font-weight: 500;
  width: 180px;
  font-size: 16px;
  line-height: 40px;
  text-align: right;
}

.add-update-preview .el-form-item /deep/ .el-form-item__content {
  margin-left: 180px;
}

.add-update-preview .el-input /deep/ .el-input__inner {
  border: 1px solid #5479bb;
  border-radius: 4px;
  padding: 0 12px;
  box-shadow: 1px 2px 3px #eee;
  outline: none;
  color: #aaa;
  width: 400px;
  font-size: 14px;
  height: 40px;
}

.add-update-preview .el-select /deep/ .el-input__inner {
  border: 1px solid #5479bb;
  border-radius: 4px;
  padding: 0 10px;
  box-shadow: 1px 2px 3px #eee;
  outline: none;
  color: #aaa;
  width: 200px;
  font-size: 14px;
  height: 40px;
}

.add-update-preview .el-date-editor /deep/ .el-input__inner {
  border: 1px solid #5479bb;
  border-radius: 4px;
  padding: 0 10px 0 30px;
  box-shadow: 1px 2px 3px #eee;
  outline: none;
  color: #aaa;
  width: 200px;
  font-size: 14px;
  height: 40px;
}

.add-update-preview /deep/ .el-upload--picture-card {
  background: transparent;
  border: 0;
  border-radius: 0;
  width: auto;
  height: auto;
  line-height: initial;
  vertical-align: middle;
}

.add-update-preview /deep/ .upload .upload-img {
  border: 1px dashed #5479bb;
  cursor: pointer;
  border-radius: 6px;
  color: #5479bb;
  width: 200px;
  font-size: 32px;
  line-height: 120px;
  text-align: center;
  height: 120px;
}

.add-update-preview /deep/ .el-upload-list .el-upload-list__item {
  border: 1px dashed #5479bb;
  cursor: pointer;
  border-radius: 6px;
  color: #5479bb;
  width: 200px;
  font-size: 32px;
  line-height: 120px;
  text-align: center;
  height: 120px;
}

.add-update-preview /deep/ .el-upload .el-icon-plus {
  border: 1px dashed #5479bb;
  cursor: pointer;
  border-radius: 6px;
  color: #5479bb;
  width: 200px;
  font-size: 32px;
  line-height: 120px;
  text-align: center;
  height: 120px;
}

.add-update-preview .el-textarea /deep/ .el-textarea__inner {
  border: 1px solid #5479bb;
  border-radius: 4px;
  padding: 12px;
  box-shadow: 1px 2px 3px #eee;
  outline: none;
  color: #aaa;
  width: 400px;
  font-size: 14px;
  height: auto;
}
</style>
